﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Adams.Admin.Business.DailyPrayers
{
    public static class CreatePrayer
    {
        public static Adams.Admin.DataTransfer.DailyPrayers.CreatePrayer.PageLoad PageLoad(int id, int month)
        {
            if (month == 0)
            {
                month = DateTime.Now.Month;
            }

            var data = DataAccess.DailyPrayers.CreatePrayer.PageLoad(id, month);
            data.Month = month;
            return data;
        }

        public static DataTransfer.FormOutput SavePrayers(DataTransfer.DailyPrayers.CreatePrayer.FormInput input)
        {
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                HijriCalendar myCal = new HijriCalendar();
                foreach (var prayertime in input.PrayerTimes)
                {
                    var d = new DateTime(DateTime.Now.Year, prayertime.Month, prayertime.Day);
                    prayertime.IMonth = myCal.GetMonth(d.AddDays(Shared.Config.GetAppSetting("HijriAdjust").ToInt()));
                    prayertime.IDay = myCal.GetDayOfMonth(d.AddDays(Shared.Config.GetAppSetting("HijriAdjust").ToInt()));
                }

                DataAccess.DailyPrayers.CreatePrayer.Save(input);
                output.IsSaved = true;
            }

            return output;
        }
    }
}
